/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ecom.tags;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;

/**
 *
 * @author simon
 */
public class ExceptionTag extends SimpleTagSupport {

    @Override
    public void doTag() throws JspException, IOException {
        JspWriter out = getJspContext().getOut();
        try {
            
            //getJspContext().
            Throwable err = (Throwable) getJspContext().getAttribute("javax.servlet.error.exception", PageContext.REQUEST_SCOPE);

            if (err != null) {
                if (err instanceof ServletException) {
                    // It's a ServletException: we should extract the root cause
                    ServletException se = (ServletException) err;
                    Throwable rootCause = se.getRootCause();
                    if (rootCause == null) {
                        rootCause = se;
                    }
                    out.println("** Root cause is: " + rootCause.getMessage());
                } else {
                    // It's not a ServletException, so we'll just show it
                    out.println("Exception : " + err.toString());
                    out.println("<br />" + err.getCause().getLocalizedMessage());
                }
            } else {
                out.println("No error information available");
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }
}
